import 'package:flutter/material.dart';

import '../../../../lib_baseview/widget/dialogs.dart';
import '../../../../lib_user/user_manager.dart';
import '../../interface/chat_room_leaf_interface.dart';

class MicWaitingListPageModel extends ChatRoomChangeNotifier {
  // Channel? get channel {
  //   // rootProxyModel.channel?.waitQueue.addAll(micWaitingList);

  //   // try{
  //   //   rootProxyModel.channel;
  //   // }catch(e){
  //   //   return null;
  //   // }
  //   return roomModel.channel;
  // }

  bool get isInWaitingQueue {
    // return roomModel.channel?.waitQueue
    //         .map((e) => e.user.id.toInt())
    //         .contains(UserManager.instance.currentUser?.id ?? 0) ??
    //     false;
    return false;
  }

  bool get isInCarOwnerMicQueue {
    // return roomModel.channel?.carOwnerMicQueue
    //         .map((e) => e.user.id.toInt())
    //         .contains(UserManager.instance.currentUser?.id ?? 0) ??
    //     false;
    return false;
  }

  bool get showDownMic {
    return (tabController == null && isInWaitingQueue) ||
        (tabController != null &&
            ((tabController!.index == 0 && isInCarOwnerMicQueue) ||
                (tabController!.index == 1 && isInWaitingQueue)));
  }

  /// 是否在排麦列表
  bool get inQueue => isInCarOwnerMicQueue || isInWaitingQueue;

  /// 是否在指定类型的排麦列表
  // bool inQueueOfType(QueueType type) =>
  //     type == QueueType.COMMON_QUEUE ? isInWaitingQueue : isInCarOwnerMicQueue;

  // bool inQueueOfType(QueueType type) => isInCarOwnerMicQueue;

  TabController? tabController;

  initTabController(
      SingleTickerProviderStateMixin singleTickerProviderStateMixin) {
    // if ((channel?.roomTypeObj.type == RoomType.HOT_HALL) &&
    //     tabController == null) {
    //   tabController =
    //       TabController(length: 2, vsync: singleTickerProviderStateMixin);
    //   tabController?.addListener(_onTabChange);
    // }
  }

  void _onTabChange() => notifyListeners();

  releaseTabController() {
    tabController?.removeListener(_onTabChange);
    tabController?.dispose();
  }

  @override
  void dispose() {
    releaseTabController();
    super.dispose();
  }

  /// 排麦
  void joinMicQueue(BuildContext context) async {
    // ChannelUpdate req = ChannelUpdate.create();
    // req.channel = channel!.channelId;

    // QueueType queueType = QueueType.COMMON_QUEUE;
    // if (tabController != null && tabController!.index == 0) {
    //   queueType = QueueType.CAR_OWNER_QUEUE;
    // }
    // req.upMic = ChannelUpMic.create()
    //   ..mode = UpMicMode.QUEUE_MIC
    //   ..queueType = queueType;
    // var result = await RtcApi.instance.updateChannel(req);
    // if (result.isSuccess) {
    // }
    // else if(result.errorCode == 1001410212){
    //   String tempStr = "驾驶座驾才可上麦与嘉宾互动，您当前未拥有座驾，是否立即获取？";
    //   if (UserManager.instance.currentUser?.openedBlindBox ?? false) {
    //     tempStr = "驾驶座驾才可上麦与嘉宾互动，您当前座驾已过期，是否立即获取新座驾?";
    //   }
    //   RoomDialogUtils.showNoValidCarDialog(context: context,tittle2: tempStr);
    // }
    // else {
    showToast('result.errorMsg');
    // }
  }

  /// 取消排麦
  void cancelMicQueue() async {
    // ChannelUpdate req = ChannelUpdate.create();
    // req.channel = channel!.channelId;

    // QueueType queueType = QueueType.COMMON_QUEUE;
    // if (tabController != null && tabController!.index == 0) {
    //   queueType = QueueType.CAR_OWNER_QUEUE;
    // }
    // req.cancelQueueMic = ChannelCancelQueueMic.create()..queueType = queueType;
    // var result = await RtcApi.instance.updateChannel(req);
    // if (result.isSuccess) {
    // } else {
    //   showToast(result.errorMsg ?? "");
    // }
  }

  _openModalBottomSheet(BuildContext context, WidgetBuilder builder) {
    showModalBottomSheet(
      isScrollControlled: true,
      constraints: BoxConstraints(
          maxHeight: MediaQuery.of(context).size.height -
              MediaQuery.of(context).viewPadding.top),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(24),
          topRight: Radius.circular(24),
        ),
      ),
      context: context,
      builder: builder,
    );
  }

  // openMicSelectPage(BuildContext context, SimpleChannelUserSlot channelUser) {
  //   _openModalBottomSheet(
  //       context,
  //       (context) => MicSelectPage(
  //             channelUser: channelUser.user,
  //           ));
  // }

  // void quitMic(SimpleChannelUserSlot? seatInfo) async {
  //   roomModel.sendMessage(RoomConstant.MESSAGE_ID_QUIT_MIC, data: seatInfo);
  // }
}
